﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Strive.DataAccess.Base.Model.PageObject;
using Strive.DataAccess.Base.Model.SearcherObject;

using OTA.Notification.BO.Message;
using OTA.Notification.Entity.Model.Message;

namespace OTA.Notification.Web.Message
{

    public class RepeatMessageBO : RepeatMsgBo
    {

        protected override void ValidationRules()
        {
            //
        }
    }



    public partial class RepeatMsgList : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                InitPage();

                if (Request.QueryString["action"] != null)
                {
                    //get data
                    if (Request.QueryString["action"].ToLower() == "get")
                    {
                        int iPageIndex = 1;
                        int iPageSize = 15;

                        int iState = 1;
                        string strQuery = string.Empty;

                        if (!string.IsNullOrEmpty(Request.Params["page"]))
                            int.TryParse(Request.Params["page"], out iPageIndex);

                        if (!string.IsNullOrEmpty(Request.Params["rp"]))
                            int.TryParse(Request.Params["rp"], out iPageSize);

                        if (!string.IsNullOrEmpty(Request.Params["query"]))
                            strQuery = Request.Params["query"];

                        Response.Write(GetJSON(iPageIndex, iState, strQuery, iPageSize));
                        Response.End();

                    }
                    else if (Request.QueryString["action"].ToLower() == "del")
                    {

                        if (!string.IsNullOrEmpty(Request.Params["key1"]))
                        {
                            int iID = 0;

                            int.TryParse(Request.Params["key1"], out iID);

                            bool b = DoDelete(iID);

                            if (b)
                                Response.Write("{state:1}");
                            else
                                Response.Write("{state:0}");

                            Response.End();

                        }
                    }

                }

            }
        }

        private bool DoDelete(int Key1)
        {
            try
            {
                using (WebMessageBO bo = new WebMessageBO())
                {

                    WebMsgBean bean = bo.Load(Key1);

                    if (bean != null)
                    {
                        // bo.MarkOld();
                        bo.Delete();

                        bo.Save(bean);
                    }


                }
            }
            catch
            {
                return false;
            }

            return true;
        }


        private void InitPage()
        {

        }

        /// <summary>
        /// 解析查询
        /// </summary>
        /// <param name="Query"></param>
        /// <param name="Room"></param>
        /// <param name="State"></param>
        private void ParseQuery(string Query, out string Room, out int State)
        {
            string[] sp = Query.Split(',');

            State = -1;
            Room = string.Empty;

            if (sp.Length == 2)
            {
                Room = sp[0];
                int.TryParse(sp[1], out State);
            }
        }


        private string GetJSON(int PageIndex, int State, string Query, int PageSize)
        {

            string strJson = "";

            using (RepeatMessageBO bo = new RepeatMessageBO())
            {

                BasePageSearch bs = new BasePageSearch();

                bs.PageSize = PageSize;
                bs.PageIndex = PageIndex;
                bs.Column = "*";

                if (!string.IsNullOrEmpty(Query))
                {
                    Query = Query.Replace("'", "''");

                    bs.Where = " and CLIENT_TX_SEQ = '" + Query + "' ";
                }

                bs.Where = " AND REPEAT_EVENT_STATUS = 1 ";

                //bs.Order = " order by MSG_REPEAT_EVENT_ID DESC";

                PageResult<RepeatMsgBean> pr = bo.SelectPageList(bs); //update by kevin 6.25

                int iViewStatePageCount = 0;

                if (Session["SESSION_PAGE_COUNT"] != null)
                    int.TryParse(Session["SESSION_PAGE_COUNT"].ToString(), out iViewStatePageCount);

                if (pr.RecordCount == 0)
                    pr.RecordCount = iViewStatePageCount;
                else
                    Session["SESSION_PAGE_COUNT"] = pr.RecordCount;

                strJson = "{\"total\":" + pr.RecordCount + ",\"page\":" + PageIndex.ToString() + ",\"rows\":[";

                foreach (RepeatMsgBean bean in pr.Data)
                {

                    strJson += "{\"id\":\"" + bean.ID.ToString() + "\",";
                    strJson += "\"cell\":[";
                    strJson += "\"" + HttpUtility.HtmlEncode(bean.ClientTXSeq) + "\",";
                    strJson += "\"" + HttpUtility.HtmlEncode(bean.AppCode) + "\",";
                    strJson += "\"" + HttpUtility.HtmlEncode(bean.MsgSendType) + "\",";
                    strJson += "\"" + HttpUtility.HtmlEncode(bean.Sender) + "\",";

                    strJson += "\"" + HttpUtility.HtmlEncode(bean.NextStartTime.ToString()) + "\",";

                    strJson += "\"" + bean.RepeatTimes.ToString() + "\"";

                    strJson += "]},";
                }

            }

            strJson = strJson.Replace("\r\n", " ");

            strJson = strJson.TrimEnd(',');
            strJson += "]}";

            return strJson;
        }
    }
}
